<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\common\config\model\ConfigStore;
use app\common\product\model\StoreBanner;
use fast\WechatApp;
use think\Request;

class Index
{
    protected $request = null;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function login()
    {
        $post = $this->request->post();
        $code = $post['code'];
        $model = new WechatApp();
        $res = $model->login($code);

        return success($res, '登录成功');
    }

    public function index()
    {
        $bannerModel = new StoreBanner();
        $where = [];
        $where[] = ['status', '=', 1];
        $whereRaw = "(start_time <= ? AND end_time >= ?) OR forever = 1";
        $bannerList = $bannerModel->where($where)->whereRaw($whereRaw, [time(), time()])->order('sort')->select();
        $result['swiper'] = $bannerList;

        // 获取门店列表
        $storeModel = new ConfigStore();
        $storeList = $storeModel->where('status',1)->select();
        // 添加一个前端需要的距离字段
        foreach ($storeList as $store) {
            $store->append(['zone']);
            $store['distance'] = 0;
        }
        $result['storeList'] = $storeList;

        return success($result, '获取数据成功');
    }
}
